<template>
  <div class="app-container">
    <el-form :model="searchdata" ref="searchdata" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客资姓名" prop="kzName">
        <el-input v-model="searchdata.kzName" placeholder="请输入客资姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phonenumber">
        <el-input v-model="searchdata.kzTel" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否成交" prop="chengjiao">
        <el-select v-model="searchdata.chengjiao" placeholder="是否成交" clearable style="width: 150px">
          <el-option v-for="dict in chengjiao" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="服务种类" prop="kzName">
        <el-select v-model="searchdata.serveType" placeholder="服务种类" clearable style="width: 150px">
          <el-option v-for="dict in serveType" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="录入时间">
        <el-date-picker v-model="searchdata.kzCreateTime" type="date" placeholder="开始时间" style="width: 150px">
        </el-date-picker>
        <el-date-picker v-model="searchdata.kzEndTime" type="date" placeholder="结束时间" style="width: 150px">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handlesearch(searchdata)">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['puser:userlist:followkz:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelete(select)"
          v-hasPermi="['puser:userlist:followkz:del']">删除</el-button>
      </el-col>
    </el-row>
    <!-- <div class="tongji">
      <h2 class="title">统计</h2>
    </div> -->
    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客资ID" align="center" prop="kzId" />
      <el-table-column label="客资编号" align="center" prop="kzResource" />
      <el-table-column label="客资姓名" align="center" prop="kzName" />
      <el-table-column label="联系方式" align="center" prop="kzTel" />
      <el-table-column label="微信号" align="center" prop="kzWechat" />
      <el-table-column label="服务种类" align="center" prop="serveType" />
      <el-table-column label="客资来源" align="center" prop="kzFrom" />
      <el-table-column label="创建时间" align="center" prop="kzCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.kzCreateTime, '{YY}-{MM}-{DD} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="rTypeId">
        <template slot-scope="scope">
          <span v-if="scope.row.rTypeId == '0'">待审核</span>
          <span v-else-if="scope.row.rTypeId == '1'">待审核</span>
          <span v-else-if="scope.row.rTypeId == '2'">无效</span>
          <span v-else>有效</span>
        </template>
      </el-table-column>
      <el-table-column label="是否分配" align="center" prop="rTypeId">
        <template slot-scope="scope">
          <span v-if="scope.row.rTypeId == '0'">待分配商家</span>
          <span v-else-if="scope.row.rTypeId == '1'">待分配商家</span>
          <span v-else-if="scope.row.rTypeId == '2'">待分配商家</span>
          <span v-else-if="scope.row.rTypeId == '5'">待分配商家</span>
          <span v-else-if="scope.row.rTypeId == '3'">已分配商家</span>
          <span v-else-if="scope.row.rTypeId == '4'">已分配商家</span>
          <span v-else-if="scope.row.rTypeId == '6'">已分配商家</span>
          <span v-else-if="scope.row.rTypeId == '7'">已分配商家</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加客资 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客资姓名" prop="kzName">
          <el-input v-model="form.kzName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="kzTel">
          <el-input v-model="form.kzTel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="微信号" prop="kzWechat">
          <el-input v-model="form.kzWechat" placeholder="请输入客资微信号" />
        </el-form-item>
        <el-form-item label="服务种类" prop="serveType">
          <el-select v-model="form.serveType" placeholder="请输入客资来源" clearable style="width: 150px">
            <el-option v-for="dict in serveType" :key="dict.key" :label="dict.label" :value="dict.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="客资来源" prop="kzFrom">
          <el-select v-model="form.kzFrom" placeholder="请输入客资来源" clearable style="width: 150px">
            <el-option v-for="kz in kzFrom" :key="kz.key" :label="kz.label" :value="kz.label" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { handleFollowKZ, getkzlist, search, insertkzlist, deletekzlist } from "@/api/puser/userlist.js";
// import axios from "axios";
export default {
  name: "User",
  data() {
    return {
      select: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: '',
      // 是否成交
      chengjiao: [{
        value: '0',
        label: '成交'
      }, {
        value: '1',
        label: '未成交'
      }],
      // 服务种类
      serveType: [{ key: 1, label: '婚纱礼服' }, { key: 2, label: '婚纱策划' }, { key: 3, label: '婚纱礼服' }, { key: 4, label: '婚纱摄影' }],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 【请填写功能名称】表格数据
      userList: [],
      // 搜索
      searchdata: {
        kzName: '',
        kzCreateTime: '',
        kzEndTime: '',
        kzTel: '',
        serveType: '',
        chengjiao: '',
        userid: this.$route.query.userid,
      },
      // 查询参数
      queryParams: {
        kzName: null,
        kzCreateTime: null,
        kzEndTime: null,
        kzTel: null,
        serveType: null,
        chengjiao: null,
        kzFrom: null,
        kzFrom: null,
      },
      kzFrom: [{
        key: 1,
        label: '主动咨询'
      }, {
        key: 2,
        label: '手动留资'
      }, {
        key: 3,
        label: '分享留资'
      }, {
        key: 4,
        label: '后台录入'
      }, {
        key: 5,
        label: '异业录入'
      }],
      // 表单参数
      form: {
        kzName: '',
        kzTel: '',
        kzWechat: '',
        serveType: '',
        kzFrom: '',
      },
      // 表单校验
      rules: {
        kzName: [
          { required: true, message: "客资姓名不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        kzTel: [
          { required: true, message: "客资电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        kzWechat: [
          { required: true, message: "客资微信号不能为空", trigger: "blur" },
          { min: 6, max: 20, message: '客资微信号长度必须介于6到20之间', trigger: 'blur' }
        ],
        serveType: [
          { required: true, message: "服务种类不能为空", trigger: "blur" }
        ],
        kzFrom: [
          { required: true, message: "客资来源不能为空", trigger: "blur" },
        ]
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        kzName: null,
        kzTel: null,
        kzWechat: null,
        serveType: null,
        kzFrom: null,
      };
      this.resetForm("form");
    },
    // 多选框选中操作
    handleSelectionChange(selection) {
      let that = this;
      this.kzId = selection.map(item => item.kzId);
      that.select = this.kzId;
    },
    /** 查询客资列表 */
    async getList(query) {
      this.loading = true;
      let that = this;
      let res = await getkzlist(this.searchdata);
      if (res.code == 200) {
        this.userList = res.data.data;
        this.total = res.data.length;
        this.loading = false;
        if (this.userList != undefined) {
          for (let i = 0; i < that.userList.length; i++) {
            that.avatar = that.userList[i].avatar;
          }
        }
      }
    },
    /** 搜索按钮操作 */
    handlesearch(searchdata) {
      search(searchdata).then(response => {
        this.userList = response.data.data;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.searchdata = {
        kzName: '',
        kzCreateTime: '',
        kzEndTime: '',
        kzTel: '',
        serveType: '',
        chengjiao: '',
        userid: this.$route.query.userid,
      };
      this.getList()
    },
    // 新增按钮
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客资";
    },
    // 删除按钮
    handleDelete(select) {
      deletekzlist(select).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      })
    },
    // 提交按钮
    submitForm() {
      insertkzlist(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },

}
</script>
<style scoped>
.tongji {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  flex-direction: row;
  justify-content: space-between;
}
</style>